const {
  myApi,
  home
} = require('../../api/index')
const app = getApp()
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    draw: false,
  },

  /**
   * 组件的初始数据
   */
  data: {
    drawActive: false,
    IMGBASEURL: app.globalData.IMGBASEURL,
    signList: [],
    inspiration: 0,
    img: "",
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      this.singKey(1)
      this.qRCode()
    },
  },
  methods: {
    // 保存二维码
    saveImag(e) {
      console.log();
      var imgSrc = e.currentTarget.dataset.img
      wx.downloadFile({
        url: imgSrc,
        success: function (res) {
          console.log(res);
          //图片保存到本地
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: function (data) {
              wx.showToast({
                title: '保存成功',
                icon: 'none',
                duration: 2000
              })
            },
            fail: function (err) {
              wx.showToast({
                title: '保存失败，点击右上角打开设置，打开添加相册权限',
                icon: 'none',
                duration: 2000
              })
            },
            complete(res) {
              console.log(res);
            }
          })
        }
      })
    },
    // 获取二维码
    qRCode() {
      home.getbannerinfolist({
        BannerInfoQueryParams: {
          BannerInfoGroupType: "QrCode",
          ClientType: 3,
        },
      }).then((res => {
        if (res.Result.length>0) {
          this.setData({
            img:res.Result[0]?.BannerUrl
          })
        }
      }))
    },
    //加入社群
    joiAssociation() {
      wx.navigateTo({
        url: '/pages/my/association/association',
      })
    },
    succeedClose() {
      this.setData({
        drawActive: false
      })
    },
    inspirationClose() {
      this.triggerEvent('eventName', false)
    },
    singKey(item) {
      if (app.globalData.userStatus.UserInfoStatus !== 0) {
        return
      }
      myApi.usersigndetail({}).then((res => {
        if (item == 1) {
          res.Result.SignInfo.forEach((value) => {
            if (value.Today) {
              if (value.Sign) {
                this.setData({
                  draw: false
                })
              } else {
                this.setData({
                  draw: true
                })
              }
            }
          })
        }
        this.setData({
          signList: res.Result
        })
      }))
    },
    //立即签到
    immediatelyBtn() {
      this.data.signList.SignInfo.forEach((vallue) => {
        if (vallue.Today) {
          if (vallue.Sign) {
            wx.showToast({
              title: '今日已签到',
              icon: "none"
            })
          } else {
            myApi.signIn({}).then((res) => {
              this.singKey()
              this.setData({
                inspiration: wx.getStorageSync('userInfo').Telepathy + (res.Result.TelepathyValue || 0),
                drawActive: true,
                presentInspiration: res.Result.TelepathyValue
              })
              this.triggerEvent('singtName', {
                draw: false,
                inspiration: this.data.inspiration
              })
              let userInfo = wx.getStorageSync('userInfo')
              userInfo.Telepathy = this.data.inspiration
              wx.setStorageSync('userInfo', userInfo)
            })
          }
        }
      })
    },
  },

})